import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * Company - 租户级权限维护tab页 - 采购物料 model
 * @date: 2018-7-31

 */
import { getResponse } from 'utils/utils';
import { deleteData, queryData, saveData } from "../../services/authorityManagementService";
export default {
  namespace: 'authorityPurchasing',
  state: {
    head: {},
    data: {
      list: []
    }
  },
  effects: {
    fetch: /*#__PURE__*/_regeneratorRuntime.mark(function fetch(_ref, _ref2) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetch$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(queryData, payload);

            case 4:
              response = _context.sent;
              data = getResponse(response);

              if (!data) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'queryauthorityPurchasing',
                payload: data
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetch);
    }),
    add: /*#__PURE__*/_regeneratorRuntime.mark(function add(_ref3, _ref4) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function add$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(saveData, payload);

            case 4:
              response = _context2.sent;
              return _context2.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, add);
    }),
    delete: /*#__PURE__*/_regeneratorRuntime.mark(function _delete(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function _delete$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(deleteData, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, _delete);
    })
  },
  reducers: {
    queryauthorityPurchasing: function queryauthorityPurchasing(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        data: _objectSpread({
          list: action.payload.userAuthorityLineList.content
        }, action.payload.userAuthorityLineList),
        head: action.payload.userAuthority
      });
    },
    addNewData: function addNewData(state) {
      return _objectSpread(_objectSpread({}, state), {}, {
        data: _objectSpread(_objectSpread({}, state.data), {}, {
          size: state.data.list.length >= state.data.size ? state.data.size + 1 : state.data.size,
          totalElements: state.data.totalElements + 1
        })
      });
    },
    removeNewAdd: function removeNewAdd(state) {
      return _objectSpread(_objectSpread({}, state), {}, {
        data: _objectSpread(_objectSpread({}, state.data), {}, {
          size: state.data.list.length >= state.data.size ? state.data.size - 1 : state.data.size,
          totalElements: state.data.totalElements - 1
        })
      });
    },
    editRow: function editRow(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        data: _objectSpread(_objectSpread({}, state.data), {}, {
          list: action.payload.data
        })
      });
    },
    updateState: function updateState(state, _ref7) {
      var payload = _ref7.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};